package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.AdminService;
import com.atguigu.ssyx.common.utils.MD5;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.result.Result;
import com.atguigu.ssyx.vo.acl.AdminQueryVo;
import com.atguigu.ssyx.vo.user.UserQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @Author: Ian
 * @Date: 2024/3/13 19:34
 */
@CrossOrigin
@Api(tags = "用户接口")
@Slf4j
@RestController
@RequestMapping("/admin/acl/user")
public class AdminController {

    @Autowired
    private AdminService adminService;


    /**
     * 分页查询用户
     * @param pageNum
     * @param pageSize
     * @param adminQueryVo
     * @return
     */
    @GetMapping("/{pageNum}/{pageSize}")
    @ApiOperation("分页查询用户列表")
    public Result queryPageAdmin(@PathVariable Long pageNum,
                                @PathVariable Long pageSize,
                                AdminQueryVo adminQueryVo
            ){
        log.info("分页查询用户: 当前页:" + pageNum + ", 每页数量:" + pageSize + ", 查询条件:" + adminQueryVo);
        // 封装page对象
        Page<Admin> page = new Page<>(pageNum,pageSize);

        IPage<Admin> iPage = adminService.queryPageAdmin(page, adminQueryVo);

        return Result.ok(iPage);
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @GetMapping("/get/{}")
    @ApiOperation("根据id查询用户")
    public Result queryAdminById(@PathVariable Long id){
        log.info("根据id查询用户 id:" + id );
        Admin admin = adminService.getById(id);
        return Result.ok(admin);
    }

    /**
     * 新建用户
     * @param admin
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("新建用户")
    public Result save(@RequestBody Admin admin){
        // 将用户密码加密
        admin.setPassword(MD5.encrypt(admin.getPassword()));

        adminService.save(admin);

        return Result.ok(null);
    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @ApiOperation("根据id删除用户")
    public Result removeAdminById(@PathVariable("id") Long id){
        adminService.removeById(id);
        return Result.ok(null);
    }

    /**
     * 批量删除用户
     * @param list
     * @return
     */
    @DeleteMapping("/batchRemove")
    @ApiOperation("批量删除用户")
    public Result batchRemove(@RequestBody List<Long> list){
        adminService.removeByIds(list);
        return Result.ok(null);
    }

    /**
     * 根据id修改用户信息
     * @param admin
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("修改用户")
    public Result update(@RequestBody Admin admin){

        adminService.updateById(admin);
        return Result.ok(null);
    }
}
